<?php
/**
 * 作者管理控制器
 * 
 * @author sandy
 *
 */
class authorApp extends adminControl {
	
	protected function _init(){
		$this->lang->load('author');
	}
	
	public function index(){
		
		$author_mod = &get_model('author');
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		$author = $author_mod->get_all_author($page_info);
		
		// 获取分页信息
		$page_info['total'] = $author['total'];
		$url = "index.php?c=author";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('author',$author);
		$this->assign('pages',$pages);
		$this->_title = $this->lang->get('html_title') . ' - ' . $this->lang->get('app_name');
		$this->display();
	}
	
	
	public function add(){
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			$author_mod = &get_model('author');
			$author = isset($_POST['name']) ? trim($_POST['name']) : '';
			$letter = isset($_POST['letter']) ? trim($_POST['letter']) : '';
			$author_info = array(
				'author_name' => $author,
				'letter' => $letter
			);
			
			$return_url = $this->config->get_item('site_url').'/index.php?c=author';
			if ($author_mod->add_author($author_info)){
				$this->show_message( "添加作者成功!",$return_url);
			} else {
				$this->show_message( "添加作者失败!",$return_url);
			}
		} else {
			$this->display();
		}
	}
	
	public function edit() {
		$author_id = isset($_GET['aid']) ? intval($_GET['aid']) : 0;
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		$author_mod = &get_model('author');
		
		if ($dosubmit){		
			$author = isset($_POST['name']) ? trim($_POST['name']) : '';
			$letter = isset($_POST['letter']) ? trim($_POST['letter']) : '';
			$author_id = isset($_POST['author_id']) ? intval($_POST['author_id']) : '';
			
			$author_info = array(
				'author_id' => $author_id,
				'author_name' => $author,
				'letter' => $letter
			);
			
			$return_url = $this->config->get_item('site_url').'/index.php?c=author&a=edit&aid='.$author_id;
			if ($author_mod->update_author($author_info)){
				$this->show_message( "更新作者信息成功!",$return_url);
			} else {
				$this->show_message( "更新作者信息失败!",$return_url);
			}
			
		} else {
			
			$author_info = $author_mod->get_author($author_id);
			
			$this->assign('author_info', $author_info);
			$this->display();
		}
	}
}